//
//  ST_BTUserInfoManager+Buddy.swift
//  btvideo
//
//  Created by Jason on 2021/11/2.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import Foundation
import SwiftyJSON
import RealmSwift

//MARK: - 好友
extension ST_BTUserInfoManager{
    
    
    func delete_match(accid:String){
        
        let realm = try! Realm()
        
        let obj = realm.objects(ST_BTSweetheart.self).filter("accid == %@", accid)
        
        try! realm.write {
            realm.delete(obj)
        }
        
        let list = realm.objects(ST_BTSweetheart.self).sorted(byKeyPath: "createTime", ascending: false)
        
        self.sweetheart_list = Array(list)
        
        NotificationCenter.default.post(name: Notification.Name.refresh_sweet_heart, object: nil, userInfo: nil)
    }
    
    func receive_match(json:JSON){
        
        let h = ST_BTSweetheart.init(fromJson: json)
        
        let currTime = Date().timeIntervalSince1970
        
        h.updateDate = currTime
        h.createTime = currTime
        
        let realm = try! Realm()
        
        try! realm.write {
            realm.add(h)
        }
        
        let list = realm.objects(ST_BTSweetheart.self).sorted(byKeyPath: "createTime", ascending: false)
        
        
        
        
        
        BINP.update_base_info(remoteInfo: ST_BTUserInfoFromRemote.init(accid: h.accid, avatar: h.avatar, userName: h.nickname))
        
        self.sweetheart_list = Array(list)
        
        NotificationCenter.default.post(name: Notification.Name.refresh_sweet_heart, object: nil, userInfo: nil)
    }
    
    func refresh_my_sweetheart_list(){
        
        if self.current_account_info == nil{
            return
        }
        
        
        JVrequest(PH.matchedPersonListApi,parameters:["size":1000], success: { (json) in
            
            let json_array = json["data"].arrayValue
            
            print("当前好友个数:",json_array)
            
            let currTime = Date().timeIntervalSince1970
            
            let list = json_array.map { (item) -> ST_BTSweetheart in
                let m = ST_BTSweetheart.init(fromJson: item)
                m.updateDate = currTime
                
                
                BINP.update_base_info(remoteInfo: ST_BTUserInfoFromRemote.init(accid: m.accid, avatar: m.avatar, userName: m.nickname))
                
                return m
            }
            
            self.sweetheart_list = list
            
            NotificationCenter.default.post(name: Notification.Name.refresh_sweet_heart, object: nil, userInfo: nil)
            
            let realm = try! Realm()

            try! realm.write {
                realm.add(list)
            }
            
            let oldObjects = realm.objects(ST_BTSweetheart.self).filter("updateDate < \(currTime)")
            
            try! realm.write {
                realm.delete(oldObjects)
            }
            
            
        }) { (code, err) in
           
            print("好友列表错误❌:",err)
            
            main_thread_execute(afterDelay: 0.3) {
                
                self.refresh_my_sweetheart_list()
            }
        }
        
        
    }
    
    
    
    func clear_sweetheart_list(){
        
        self.sweetheart_list?.removeAll()
        
        NotificationCenter.default.post(name: Notification.Name.refresh_sweet_heart, object: nil, userInfo: nil)
        
        let realm = try! Realm()
        
        let oldObjects = realm.objects(ST_BTSweetheart.self)
        
        try! realm.write {
            realm.delete(oldObjects)
        }
    }
}
